package com.beta.pay.yeepay.core;

import com.beta.pay.yeepay.request.PayRequest;
import com.beta.pay.yeepay.response.PayResponse;
import com.beta.pay.yeepay.utils.ZGTUtils;
import org.apache.commons.collections.MapUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * Created by vector on 2016/11/30.
 */
public class Pays extends Component {

    private final String apiUrl = "https://o2o.yeepay.com/zgt-api/api/pay";

    public Pays(Yeepay yeepay) {
        super(yeepay);
    }

    @Override
    protected String getComponentName() {
        return "支付";
    }

    public PayResponse pay(PayRequest payRequest) {
//        logger.info("易宝支付 - 支付请求: request = {}", JSON.toJSONString(payRequest));
//        //第一步 生成密文data
//        String data = ZGTUtils.buildData(generateRequestParam(payRequest), ZGTUtils.PAYAPI_REQUEST_HMAC_ORDER);
//        //第二步 发起请求
//        Map<String, String> responseMap = ZGTUtils.httpPost(apiUrl, data);
//        logger.info("易宝支付 - 支付响应: response = {}", JSON.toJSONString(responseMap));
//        //第三步 判断请求是否成功，
//        if (responseMap.containsKey("code")) {
//            logger.error("易宝支付 - 支付失败: error = {}", JSON.toJSONString(responseMap));
//            throw new YeepayException(responseMap.get("code"), responseMap.get("msg"));
//        }
//
//        //第四步 解密同步响应密文data，获取明文参数
//        String responseData = responseMap.get("data");
//        Map<String, String> responseDataMap = ZGTUtils.decryptData(responseData);
//        logger.info("易宝支付 - 支付响应数据解密: data = {}", JSON.toJSONString(responseDataMap));
//
//        //第五步 code=1时，方表示接口处理成功
//        if (!"1".equals(responseDataMap.get("code"))) {
//            logger.error("易宝支付 - 支付业务错误: error = {}", JSON.toJSONString(responseDataMap));
//            throw new YeepayException(responseDataMap.get("code"), responseDataMap.get("msg"));
//        }
//
//        //第六步 hmac签名验证
//        if (!ZGTUtils.checkHmac(responseDataMap, ZGTUtils.PAYAPI_RESPONSE_HMAC_ORDER)) {
//            logger.error("易宝支付 - hmac校验失败: error = {}", JSON.toJSONString(responseDataMap));
//            throw new YeepayException(responseDataMap.get("code"), "hmac校验失败");
//        }
        Map<String, String> responseDataMap = fetchData(apiUrl, generateRequestParam(payRequest),
                ZGTUtils.PAYAPI_REQUEST_HMAC_ORDER, ZGTUtils.PAYAPI_RESPONSE_HMAC_ORDER);
        return buildPayResponse(responseDataMap);
    }

    public Map<String, String> decodeApiResponse(String responseData) {
        return decodeData(responseData, ZGTUtils.PAYAPICALLBACK_HMAC_ORDER);
    }

    private PayResponse buildPayResponse(Map<String, String> responseDataMap) {
        PayResponse payResponse = new PayResponse();
        payResponse.setRequestId(MapUtils.getString(responseDataMap, "requestid"));
        payResponse.setExternalId(MapUtils.getString(responseDataMap, "externaild"));
        payResponse.setAmount(MapUtils.getString(responseDataMap, "amount"));
        payResponse.setPayUrl(MapUtils.getString(responseDataMap, "payurl"));
        payResponse.setHmac(MapUtils.getString(responseDataMap, "hmac"));
        return payResponse;
    }

    private Map<String, String> generateRequestParam(PayRequest payRequest) {
        Map<String, String> requestMap = new HashMap<>();
        requestMap.put("requestid", payRequest.getRequestId());
        requestMap.put("amount", payRequest.getAmount().toString());
        requestMap.put("productname", payRequest.getProductName());
        requestMap.put("productdesc", payRequest.getProductName());
        requestMap.put("callbackurl", Objects.toString(payRequest.getCallbackUrl(), yeepay.getPayCallbackUrl()));
        requestMap.put("webcallbackurl", Objects.toString(payRequest.getWebCallbackUrl(), yeepay.getWebPayCallbackUrl()));
        requestMap.put("orderexpdate", Objects.toString(payRequest.getInvalidMinutes(), ""));
        requestMap.put("userno", payRequest.getUserNo());
        requestMap.put("payproducttype", payRequest.getPayProductType().code);
        requestMap.put("ip", payRequest.getClientIp());
        return requestMap;
    }

}
